package com.yls.except.util;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HexString {
	/*
	 * 16进制数字字符集
	 */
	private static String hexString = "0123456789ABCDEF";

	/*
	 * 将字符串编码成16进制数字,适用于所有字符（包括中文）
	 */
	public static String encode(String str) {
		// 根据默认编码获取字节数组
		byte[] bytes = str.getBytes();
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		// 将字节数组中每个字节拆解成2位16进制整数
		for (int i = 0; i < bytes.length; i++) {
			sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
			sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	/*
	 * 将16进制数字解码成字符串,适用于所有字符（包括中文）
	 */
	public static String decode(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2) {
			try {
				baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
						.indexOf(bytes.charAt(i + 1))));
			} catch (Exception e) {
				return null;
			}
		}
		return new String(baos.toByteArray());
	}

	/**
	 * 在字符串中指定位置新增指定字符串
	 * 
	 * @param str
	 *            原字符串
	 * @param num
	 *            新增位置
	 * @param repstr
	 *            指定字符串
	 * @return 新增后的字符串
	 */
	public static String insertstr(String str, int num, String repstr) {
		StringBuffer sb = new StringBuffer(str);
		sb = sb.insert(num, repstr);
		return sb.toString();
	}

	public static String removestr(String str, int num, String removestr,
			String repalestr) {
		StringBuffer sb = new StringBuffer(str);
		sb = sb.replace(num, removestr.length() + num, repalestr);
		return sb.toString();
	}
	
	/**
	 * 给获取验证电话号码加密
	 * @param phone 电话号码
	 * @param name 类型
	 * @param code 项目名称
	 * @return
	 */
	public static String phoneencode(String phone, String name, String code) {
		phone = insertstr(phone, 6, code);  //将项目名称放在电话号码的第7位
		phone = encode(phone);  //将项目名称放入电话号码后的字符串编制成16进制数字
		name = encode(name);  //将类型编制成16进制数字
		String []typename = name.split(""); //将编制成16进制数字的类型分割为单个字符
		for (int i = 0; i < typename.length; i++) {
			phone = insertstr(phone, i*2, typename[i]);  
			//将编制成16进制数字的类型分别放入编制成16进制数字的电话号码加入项目名称的相应的位置
		}
		return phone;
	}
	
	/**
	 * 获取验证码手机号解密
	 * @param phone 加密后的电话号码
	 * @param name 类型
	 * @param code 项目名称
	 * @return
	 */
	public static String phonedecode(String phone, String name, String code) {
		name = encode(name);
		String []typename = name.split("");
		for (int i = typename.length - 1; i >= 0 ; i--) {
			phone = removestr(phone, i*2, typename[i], "");
		}
		phone = decode(phone);
		phone = removestr(phone, 6, code, "");
		return phone;
	}
	
	public static void main(String[] args) {
		String item = "except";
		String uid = HexString.insertstr("8", 1, item);
		uid = HexString.encode(uid);  //加密
		System.out.println(uid);
		uid = HexString.decode(uid);
		uid = HexString.removestr(uid, 1, item, "");
		System.out.println(uid);
	}
	
	public static Map<String, Object> mapempty(Map<String, Object> map, List<String> list) {
		if(list == null || list.size() <= 0)
			return null;
		if(map == null)
			map = new HashMap<String, Object>();
		for (int i = 0; i < list.size(); i++) {
			String code = list.get(i);
			boolean flag = false;
			Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();  
			while (entries.hasNext()) {  
			    Map.Entry<String, Object> entry = entries.next();  
			    String key = entry.getKey();
			    String value = entry.getValue() + "";
			    if(code.equals(key)) {
			    	if(StringIsempty.isallempty(value))
			    		map.put(key, "");
			    	flag = true;
			    	break;
			    }
			} 
			if(!flag)
				map.put(code, "");
		}
		return map;
	}
}